import Vue from "vue";
import VueRouter from "vue-router";
import { Message } from "element-ui";
Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "Login",
    component: resolve => require(["@/components/Login.vue"], resolve),
    meta: {
      keepAlive: false // 不需要被缓存
    }
  },
  {
    path: "/Index",
    name: "Index",
    component: resolve => require(["@/views/Index.vue"], resolve),
    meta: {
      keepAlive: false
    },
    children: [
      {
        path: "/Index/ValueAdded",
        name: "ValueAdded",
        component: resolve =>
          require(["@/views/business/ValueAdded.vue"], resolve),
        meta: {
          keepAlive: false
        }
      },
      {
        path: "/Index/ValueAdded/*",
        component: resolve =>
          require(["@/views/business/ValueAdded.vue"], resolve),
        meta: {
          keepAlive: false
        }
      },
      {
        path: "/Index/NewSingleEntry",
        name: "NewSingleEntry",
        component: resolve =>
          require(["@/views/business/NewSingleEntry.vue"], resolve),
        meta: {
          keepAlive: false
        }
      },
      {
        path: "/Index/HasBeenRecorded/",
        name: "HasBeenRecorded",
        component: resolve =>
          require(["@/views/business/HasBeenRecorded.vue"], resolve),
        meta: {
          keepAlive: false
        }
      },
      // 乐业分分期
      {
        path: "/Index/ValueAddedList",
        name: "ValueAddedList",
        component: resolve =>
          require(["@/views/businessList/Index.vue"], resolve),
        meta: {
          keepAlive: false
        }
      },
      {
        path: "/Index/NewSingleEntryList",
        name: "NewSingleEntryList",
        component: resolve =>
          require(["@/views/businessList/NewSingleEntry.vue"], resolve),
        meta: {
          keepAlive: false
        }
      },
      {
        path: "/Index/HasBeenRecordedList/",
        name: "HasBeenRecordedList",
        component: resolve =>
          require(["@/views/businessList/HasBeenRecordedList.vue"], resolve),
        meta: {
          keepAlive: false
        }
      },
      //
      {
        path: "/Index/Accfund",
        name: "Accfund",
        component: resolve => require(["@/views/accfund/Accfund.vue"], resolve),
        meta: {
          keepAlive: false
        }
      },
      {
        path: "/Index/NewAccfund",
        name: "NewAccfund",
        component: resolve =>
          require(["@/views/accfund/NewAccfund.vue"], resolve),
        meta: {
          keepAlive: false
        }
      },
      {
        path: "/Index/Entered",
        name: "Entered",
        component: resolve => require(["@/views/accfund/Entered.vue"], resolve),
        meta: {
          keepAlive: false
        }
      },
      {
        path: "/Index/MortList",
        name: "MortList",
        component: resolve => require(["@/views/mortgage/Index.vue"], resolve),
        meta: {
          keepAlive: false
        }
      },
      {
        path: "/Index/NewMort",
        name: "NewMort",
        component: resolve =>
          require(["@/views/mortgage/NewMort.vue"], resolve),
        meta: {
          keepAlive: false
        }
      },
      {
        path: "/Index/EntertMort",
        name: "EntertMort",
        component: resolve =>
          require(["@/views/mortgage/Entered.vue"], resolve),
        meta: {
          keepAlive: false
        }
      },
      //法拍房
      {
        path: "/Index/FapaiList",
        name: "FapaiList",
        component: resolve => require(["@/views/fapai/Index.vue"], resolve),
        meta: {
          keepAlive: false
        }
      },
      {
        path: "/Index/NewFapai",
        name: "NewFapai",
        component: resolve => require(["@/views/fapai/NewOrder.vue"], resolve),
        meta: {
          keepAlive: false
        }
      },
      {
        path: "/Index/EntertFapai",
        name: "EntertFapai",
        component: resolve => require(["@/views/fapai/Entered.vue"], resolve),
        meta: {
          keepAlive: false
        }
      },
      // 普通按揭
      {
        path: "/Index/ordinaryMortList",
        name: "OrdinaryMortList",
        component: resolve =>
          require(["@/views/ordinaryMortgage/Index.vue"], resolve),
        meta: {
          keepAlive: false
        }
      },
      {
        path: "/Index/ordinaryNewMort",
        name: "OrdinaryNewMort",
        component: resolve =>
          require(["@/views/ordinaryMortgage/NewMort.vue"], resolve),
        meta: {
          keepAlive: false
        }
      },
      {
        path: "/Index/ordinaryEntertMort",
        name: "OrdinaryEntertMort",
        component: resolve =>
          require(["@/views/ordinaryMortgage/Entered.vue"], resolve),
        meta: {
          keepAlive: false
        }
      },
      // 融银贷！
      {
        //列表页！
        path: "/Index/MeitList",
        component: resolve =>
          require(["@/views/meitList/MeitList.vue"], resolve),
        meta: {
          keepAlive: false
        }
      },
      // 添加页！
      {
        path: "/Index/MeitListAdd",
        name: "MeitListAdd",
        component: resolve =>
          require(["@/views/meitList/MeitListAdd.vue"], resolve),
        meta: {
          keepAlive: false
        }
      },
      // 查看页！
      {
        path: "/Index/MeitListView",
        name: "MeitListView",
        component: resolve =>
          require(["@/views/meitList/MeitListView.vue"], resolve),
        meta: {
          keepAlive: false
        }
      }
    ]
  },
  {
    path: "/404",
    component: resolve => require(["@/views/error/404"], resolve),
    hidden: true
  },
  { path: "*", component: resolve => require(["@/views/error/404"], resolve) }
];

const router = new VueRouter({
  routes
});

// 路由的全局前置守卫（拦截器）--路由跳转之前执行 验证是否有携带token权限
router.beforeEach((to, from, next) => {
  let token = localStorage.getItem("token");
  // 如果是登陆时候不用判断token 根据路由name值
  // if (to.name !== "Login") {
  //   // 如果没有token，跳转登陆页面
  //   if (!token) {
  //     Message.success("请先登陆");
  //     router.push("/");
  //     return;
  //   }
  // }
  // next();
  if (to.name == "Login" || token) {
    next();
  } else {
    Message.success("请先登陆");
    router.push("/");
  }
});

export default router;
